package cn.cau.control;

import cn.cau.entity.CodeManageRecord;
import cn.cau.entity.CodeRule;
import cn.cau.entity.CommonUser;
import cn.cau.entity.RuleCategory;
import cn.cau.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
public class CodeManageAbolishController {
    @Autowired
    private CodeManageAbolishService codeManageAbolishService;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BasicEnumService basicEnumService;
    @Autowired
    private NonEnumService nonEnumService;
    @Autowired
    private CommonUserService commonUserService;
    @Autowired
    private RuleCategoryService ruleCategoryService;
    /**
     * 可废除编码列表
     *
     * @return
     */
    @RequestMapping(value = "/codeAbolishList", method = RequestMethod.GET)
    public ModelAndView codeAbolishList(HttpSession session) {
        List<CodeRule> codeRules=null;
        if (session.getAttribute("userInfo") != null) {
            //如果用户登录
            CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
            codeRules = codeManageAbolishService.showCodeRuleAbolishList(commonUser.getUserId());
        }
        ModelAndView modelAndView = new ModelAndView();
        List<RuleCategory> ruleCategories=null;
        ruleCategories=ruleCategoryService.findAllRuleCategorys();
        modelAndView.addObject("ruleCategories",ruleCategories);
        modelAndView.addObject("codeRules", codeRules);
        modelAndView.setViewName("codeManage/codeAbolishList");
        return modelAndView;
    }
    /**
     * 编码废除申请
     *
     * @return
     */
    @RequestMapping(value = "/codeAbolishInfo")
    public ModelAndView codeAbolishInfo(@RequestParam(value = "codeRuleId") int codeRuleId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("codeRule",codeRuleService.findCodeRuleById(codeRuleId));
        modelAndView.addObject("category",categoryService.findCategoryById(codeRuleId));
        modelAndView.addObject("basicEnumSums",basicEnumService.findBasicEnumSumByCodeRuleId(codeRuleId));
        modelAndView.addObject("nonEnums",nonEnumService.findNonEnumByCodeId(codeRuleId));
        modelAndView.addObject("creatorName",commonUserService.findCommonUserById(codeManageAbolishService.findRecordByNewCodeRuleId(codeRuleId).getManageManId()).getUserName());
        modelAndView.setViewName("codeManage/codeAbolishInfo");
        return modelAndView;
    }
    /**
     * 废除申请提交
     *
     * @return
     */
    @RequestMapping(value = "/codeAbolishApplication", method = RequestMethod.POST)
    public ModelAndView codeAbolishApply(HttpSession session,
                                         @RequestParam(value = "codeRuleId") int codeRuleId,
                                         @RequestParam(value = "manageContent") String manageContent){
        CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
        ModelAndView modelAndView = new ModelAndView();
        CodeRule codeRule=codeRuleService.findCodeRuleById(codeRuleId);
        codeRule.setCodeState(1);//设置为审核状态
        int result=codeRuleService.updateCodeRule(codeRule);
        if(result==1){
            CodeManageRecord codeManageRecord=new CodeManageRecord();
            codeManageRecord.setOldCodeRuleId(codeRuleId);
            codeManageRecord.setManageOperation(1);
            codeManageRecord.setManageContent(manageContent);
            codeManageRecord.setManageManId(commonUser.getUserId());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            codeManageRecord.setApplyTime(df.format(new Date()));
            result=codeManageAbolishService.submitCodeRuleAbolish(codeManageRecord);
        }
        result=result+1;
        modelAndView.addObject("result",result);
        modelAndView.setViewName("codeManage/codeManageIndex");
        return modelAndView;
    }
}
